<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData" :rules="rules">
			<uni-forms-item label="姓名" required>
				<view class="item-wrapper">{{formData.name}}</view>
			</uni-forms-item>
			<uni-forms-item name="station" label="岗位" required>
				<uni-easyinput placeholder="岗位" v-model="formData.station" trim="both" />
			</uni-forms-item>
			<uni-forms-item name="email" label="邮箱" required>
				<uni-easyinput placeholder="邮箱" v-model="formData.email" trim="both" />
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" @click="submit">提交</button>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 表单数据
				formData: {
					name: '',
					station: '',
					email: ''
				},
				rules: {
					// 对name字段进行必填验证
					name: {
						rules: [{
								required: true,
								errorMessage: '请输入姓名',
							},
							{
								minLength: 3,
								maxLength: 5,
								errorMessage: '姓名长度在 {minLength} 到 {maxLength} 个字符',
							}
						]
					},
					// 对email字段进行必填验证
					email: {
						rules: [{
							format: 'email',
							errorMessage: '请输入正确的邮箱地址',
						}]
					}
				}
			}
		},
		onLoad(e) {
			this.formData.name = e.username;
			this.formData.station = e.station;
			this.formData.email = e.email;
		},
		methods: {
			submit() {
				this.$refs.form.validate().then(res => {
					// 表单数据信息
					let that = this;
					this.request('common/user/updateUser', {
						station: that.formData.station,
						email: that.formData.email,
					}).then(res => {
						if (res.code === 0) {
							uni.showToast({
								title: res.msg,
								icon: "none",
								duration: 2000
							});
							setTimeout(() => {
								uni.navigateBack({
									delta: 1
								});
							}, 400)
						} else {
							uni.showToast({
								title: res.msg,
								icon: "none",
								duration: 2000
							});
						}
					}).catch(err => {
						that.showRejectModal = false;
						uni.showModal({
							showCancel: false,
							content: err.message || '请求服务失败'
						})
					}).finally(() => {

					})
				}).catch(err => {
					console.log('表单错误信息：', err);
					uni.showToast({
						title: '表单错误信息',
						icon: "none",
						duration: 2000
					});
				})
			}
		},
		mounted() {

		}

	}
</script>

<style>
	.item-wrapper {
		height: 100%;
		display: flex;
		align-items: center;
	}

	.uni-container {
		padding: 15px;
	}
</style>
